package com.td.filemanager;

import com.td.filemanager.business.device.DeviceManagerAbilitySlice;
import com.td.library.DistributedFileManager;
import com.td.lib.viewmodel.ViewModelAbility;
import com.td.library.media.video.ThreadPoolManager;
import com.td.library.utils.LogUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页面
 */
public class MainAbility extends ViewModelAbility {
    private static final String TAG = "MainAbility";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(DeviceManagerAbilitySlice.class.getName());

        // 动态授权
        requestPermissions();
    }

    private void requestPermissions() {
        String[] permissions = {
                // 分布式数据同步权限
                SystemPermission.DISTRIBUTED_DATASYNC,
                // 读取用户存储权限
                SystemPermission.READ_USER_STORAGE,
                // 写用户存储权限
                SystemPermission.WRITE_USER_STORAGE
        };
        List<String> permissionsToProcess = new ArrayList<>();
        boolean grantPermission = true;
        for (String permission : permissions) {
            if (verifySelfPermission(permission) != IBundleManager.PERMISSION_GRANTED) {
                grantPermission = false;
                if (canRequestPermission(permission)) {
                    permissionsToProcess.add(permission);
                }
            }
        }
        if (grantPermission) {
            readCommonFile();
        } else {
            requestPermissionsFromUser(permissionsToProcess.toArray(new String[0]), 0);
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        if (requestCode == 0) {
            boolean grantPermission = true;
            for (int result : grantResults) {
                if (result != IBundleManager.PERMISSION_GRANTED) {
                    grantPermission = false;
                }
            }
            if (grantPermission) {
                readCommonFile();
            }
        }
    }

    /**
     * 读取常规文件
     */
    public void readCommonFile() {
        DistributedFileManager distributedFileManager = DistributedFileManager.getInstance();
        if (getDistributedDir().getPath() == null) {
            new ToastDialog(getContext())
                    .setText("分布式文件路径为空，当前设备不支持分布式文件")
                    .show();
        } else {
            distributedFileManager.startUp(this, this.getAbilityPackage());
            ThreadPoolManager.getInstance().execute(() -> {
                long starTime = System.currentTimeMillis();
                distributedFileManager.copyCommonAudioFiles();
                LogUtil.warn(TAG, "copy audio files end total time:" + (System.currentTimeMillis() - starTime));
            });
            ThreadPoolManager.getInstance().execute(() -> {
                long starTime = System.currentTimeMillis();
                distributedFileManager.copyCommonImageFiles();
                LogUtil.warn(TAG, "copy image files end total time:" + (System.currentTimeMillis() - starTime));
            });
            ThreadPoolManager.getInstance().execute(() -> {
                long starTime = System.currentTimeMillis();
                distributedFileManager.copyCommonVideoFiles();
                LogUtil.warn(TAG, "copy video files end total time:" + (System.currentTimeMillis() - starTime));
            });
            ThreadPoolManager.getInstance().execute(() -> {
                long starTime = System.currentTimeMillis();
                distributedFileManager.copyCommonDownloadsFiles();
                LogUtil.warn(TAG, "copy download files end total time:" + (System.currentTimeMillis() - starTime));
            });
        }
    }
}
